package org.go.smart.statistics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class HeaterStatistics {
	
	private List<HeaterStatisticsEntry> entries = new ArrayList<HeaterStatisticsEntry>();
	
	public void addEntry() {
		entries.add(new HeaterStatisticsEntry());
	}
	
	public void endLastEntry() {
		entries.get(entries.size() - 1).endEntry();
	}
	
	public long getLastWorkingDuration() {
		return entries.get(entries.size() - 1).getDuration();
	}
	
	public long getMonthlyWorkingDuration(int year, int month) {
		long duration = 0;
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.set(year, month, 0, 0, 0, 0);
		Calendar endCalendar = Calendar.getInstance();
		if (month == 11) {
			endCalendar.set(year+1, Calendar.JANUARY, 0, 0, 0, 0);
		} else {
			endCalendar.set(year, month+1, 0, 0, 0, 0);
		}
		for (HeaterStatisticsEntry entry : entries) {
			if (startCalendar.getTime().before(entry.getStartDate()) && endCalendar.getTime().after(entry.getEndDate())) {
				duration += entry.getDuration();
			}
		}
		
		return duration;
	}
	
	
}
